package com.dense.kuiniu.floatcabinet.mvp.ui.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ScreenUtils;
import com.dense.kuiniu.floatcabinet.R;
import com.dense.kuiniu.floatcabinet.utils.timer.CountDownView;

/**
 * @author Administrator
 * @date 2020/11/3
 */
public class DialogMessage extends Dialog {
    private ImageView mImageView;
    private TextView mTvInfo;
    private TextView mTvCancel;
    private TextView mTvConfirm;
    private CountDownView mCountDownView;

    public DialogMessage(@NonNull Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_message, null);
        mImageView = view.findViewById(R.id.iv_image);
        mTvInfo = view.findViewById(R.id.tv_info);
        mTvCancel = view.findViewById(R.id.btn_cancel);
        mTvConfirm = view.findViewById(R.id.btn_confirm);
        mCountDownView = view.findViewById(R.id.view_count_down);
        setContentView(view);
    }

    public DialogMessage succeed(String info) {
        mImageView.setBackgroundResource(R.mipmap.ic_succeed);
        mTvInfo.setText(info);
        return this;
    }

    public DialogMessage failed(String info) {
        mImageView.setBackgroundResource(R.mipmap.ic_failed);
        mTvInfo.setText(info);
        return this;
    }

    public DialogMessage confirm(View.OnClickListener listener) {
        mTvConfirm.setOnClickListener(listener);
        mTvCancel.setVisibility(View.GONE);
        return this;
    }

    public DialogMessage cancel(View.OnClickListener listener) {
        mTvCancel.setVisibility(View.VISIBLE);
        mTvCancel.setOnClickListener(listener);
        return this;
    }

    public DialogMessage justInfo(String info) {
        mImageView.setVisibility(View.INVISIBLE);
        mTvConfirm.setVisibility(View.INVISIBLE);
        mTvCancel.setVisibility(View.INVISIBLE);
        mTvInfo.setText(info);
        return this;
    }

    public DialogMessage justInfoWithConfirm(String info) {
        mImageView.setVisibility(View.INVISIBLE);
        mTvConfirm.setVisibility(View.VISIBLE);
        mTvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });
        mTvCancel.setVisibility(View.INVISIBLE);
        mTvInfo.setText(info);
        return this;
    }

    private void exit() {
        if (mCountDownView != null) {
            mCountDownView.stop();
        }
        dismiss();
    }


    @Override
    public void show() {
        super.show();
        measureSize();
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    protected void measureSize() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.shape_transparent));
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = (int) (ScreenUtils.getScreenWidth() * 0.85); // 宽度
            lp.height = (int) (ScreenUtils.getScreenHeight() * 0.54); // 高度
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
            dialogWindow.setWindowAnimations(R.style.DialogOutAndInStyle);
        }
    }

}